import cv2
import numpy as np
import getpath 
import os
# 读取照片
fname=os.path.join(getpath.getpath(),"images","zp.jpg")
# 读取照片
image = cv2.imread(fname)
# 修改尺寸
image = cv2.resize(image, None, fx=0.5, fy=0.5)
# 图片转换为二值化图
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
logging.info(hsv)
# 图片的二值化黑白处理
lower_blue = np.array([90, 70, 70])
upper_blue = np.array([110, 255, 255])
heibai = cv2.inRange(hsv, lower_blue, upper_blue)
# 闭运算
k = np.ones((5, 5), np.uint8)
r = cv2.morphologyEx(heibai, cv2.MORPH_CLOSE, k)
# 颜色替换
imageNew = np.copy(image)
rows, cols, channels = image.shape
for i in range(rows):
    for j in range(cols):
        if r[i, j] == 255:  # 像素点为255表示的是白色，我们就是要将白色处的像素点，替换为红色
            imageNew[i, j] = (0, 0, 255)  # 此处替换颜色，为BGR通道，不是RGB通道
# 显示
cv2.imshow('image', image)
cv2.imshow('hsv', hsv)
cv2.imshow('heibai', heibai)
cv2.imshow('r', r)
cv2.imshow('imageNew', imageNew)
# 无限等待
cv2.waitKey(0)
# 销毁内存
cv2.destroyAllWindows()
